package com.zhengshang.common.utils;

import java.math.BigDecimal;
import java.math.BigInteger;

/**
 * @author HY
 * @version 1.0.0
 * @ClassName PromoCodeUtils.java
 * @Description TODO
 * @createTime 2021年03月03日 15:14
 */
public class PromoCodeUtils {

    //0、1、O、I不易区分的未加入
    private static final String chars = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";

    /**
     * @title 获取邀请码
     * @description
     * @author admin 
     * @updateTime 2021/3/3 15:17 
     * @throws 
     */
    public synchronized static String getPromoCode() {
        int random = (int) (Math.random() * (chars.length() * chars.length() * chars.length() * chars.length() * chars.length()));
        String code = to32String(new BigInteger(random + ""));
        return code;
    }

    /**
     * @title 10进制转换为32进制
     * @description 
     * @author admin 
     * @updateTime 2021/3/3 15:17 
     * @throws 
     */
    private synchronized static String to32String(BigInteger i) {
        BigDecimal divide = new BigDecimal(32);
        BigDecimal decimal = new BigDecimal(i);
        String res = "";
        while (BigDecimal.ZERO.compareTo(decimal) != 0){
            BigDecimal[] divRes = decimal.divideAndRemainder(divide);
            decimal = divRes[0];
            res =  chars.charAt(divRes[1].intValue()) + res;
        }
        return res;
    }
}
